﻿#region license
// CopyRight (c) 2009-2010 HangJing Teconology. All Rights Reserved.
// Function :礼品信息列表
// Created by tuhui at 2010-6-24 16:28:03.
// E-Mail: tuhui@ihangjing.com
#endregion
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Common;
using System.Collections.Generic;

public partial class qy_55tuan_Admin_DeliverList : System.Web.UI.Page
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    private string otherwhere
    {
        get
        {
            object o = ViewState["otherwhere"];
            return (o == null) ? "1=1" : o.ToString();
        }
        set
        {
            ViewState["otherwhere"] = value;
        }
    }


    Deliver dal = new Deliver();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlWhere = " 1=1 and delflag = 0 ";
            otherwhere = " 1=1";
            SqlWhere = new RightsTool().getDeliverWhere(SqlWhere);

            int revevar3 = HjNetHelper.GetQueryInt("revevar3", -1);

            if (revevar3 >= 0)
            {
                SqlWhere += " and revevar3 = '" + revevar3 + "'";
            }

            BindData();
            WebUtility.SetDDLCity(ddlcity);

            WebUtility.SetDDLGroup(ddlGpsIMEI);

        }
    }

    private void BindData()
    {
        int RecordCount = 0;
        IList<DeliverInfo> delivers = dal.GetDeliverList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "DataId", 1, otherwhere);
        if (delivers.Count > 0)
        {
            RecordCount = delivers[0].recordtcount;
        }
        this.AspNetPager1.RecordCount = RecordCount;

        this.rtpGifts.DataSource = delivers;
        this.rtpGifts.DataBind();
        AlertScript.RegScript(this.Page, this.UpdatePanel1, "init();");
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        SqlWhere = " 1=1 and delflag = 0 ";
        SqlWhere = new RightsTool().getDeliverWhere(SqlWhere);
        if (tbKeyword.Text != "")
        {
            SqlWhere += " and  Name like '%" + WebUtility.InputText(tbKeyword.Text) + "%'";
        }

        if (tbcarnum.Text != "")
        {
            SqlWhere += " and  carnum like '%" + WebUtility.InputText(tbcarnum.Text) + "%'";
        }

        if (tbusername.Text != "")
        {
            SqlWhere += " and  username like '%" + WebUtility.InputText(tbusername.Text) + "%'";
        }
        



        if (ddlcity.SelectedValue != "0")
        {
            SqlWhere += " and Inve1 = " + ddlcity.SelectedValue + "";
        }

        switch (ddlStatus.SelectedValue)
        {
            case "1":
                otherwhere = " lasttime >= '" + DateTime.Now.AddMinutes(-1 * Constant.OffLineDoor) + "'";
                break;
            case "0":

                otherwhere = "  ( lasttime <= '" + DateTime.Now.AddMinutes(-1 * Constant.OffLineDoor) + "' or lasttime is null)";

                break;
            case "-1":
                otherwhere = " 1=1 ";
                break;
        }

        if (ddlCodeId.SelectedValue != "-1")
        {
            SqlWhere += " and CodeId = '" + ddlCodeId.SelectedValue + "'";
        }

        if (ddlGpsIMEI.SelectedValue != "-1")
        {
            SqlWhere += " and GpsIMEI = " + ddlGpsIMEI.SelectedValue + "";
        }

        if (ddreveint1.SelectedValue != "-1")
        {
            SqlWhere += " and reveint1 = " + ddreveint1.SelectedValue + "";
        }



        if (ddlpost.SelectedValue != "-1")
        {
            SqlWhere += " and post = '" + ddlpost.SelectedValue + "'";
        }

        if (ddlgrade.SelectedValue != "-1")
        {
            SqlWhere += " and grade = " + ddlgrade.SelectedValue + "";
        }

        if (ddlcarower.SelectedValue != "-1")
        {
            SqlWhere += " and carower = " + ddlcarower.SelectedValue + "";
        }
        if (tbcartype.SelectedValue != "-1")
        {
            SqlWhere += " and cartype = '" + tbcartype.SelectedValue + "'";
        }
        if (ddlrevevar3.SelectedValue != "-1")
        {
            SqlWhere += " and revevar3 = '" + ddlrevevar3.SelectedValue + "'";
        }


        if (ddlordernum.SelectedValue != "-1")
        {
            SqlWhere += " and ordernum = " + ddlordernum.SelectedValue + "";
        }
        if (ddlSection.SelectedValue != "-1")
        {
            SqlWhere += " and Section = '" + ddlSection.SelectedValue + "'";
        }
        if (ddlrevevar1.SelectedValue != "-1")
        {
            SqlWhere += " and revevar1 = '" + ddlrevevar1.SelectedValue + "'";
        }




        BindData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void DelList_Click(object sender, EventArgs e)
    {
        string IdList = this.hidDels.Value;
        //判断权限
        int _rs = WebUtility.checkOperator(4);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }
        if (dal.DelOrRecoverDeliver(IdList,UserHelp.GetAdmin().AdminName,1) > 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除成功','success','true',5);init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除失败','error','true',5);init();");
        }
    }

    protected string ParseState(object x)
    {
        string temp = x.ToString();
        string rs = "";
        switch (temp)
        {
            case "0":
                rs = "离线";
                break;
            case "1":
                rs = "正常";
                break;
        }
        return rs;
    }
    protected void rtpGifts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator(4);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }
        if (e.CommandName == "Del")
        {
            if (dal.DelOrRecoverDeliver(e.CommandArgument.ToString(),UserHelp.GetAdmin().AdminName,1) > 0)
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除成功','success','true',5);init();");
                BindData();
            }
            else
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('删除失败','error','true',5);init();");
            }
        }
    }

    protected void clearGPSReclord_Click(object sender, EventArgs e)
    {
        string sql = "TRUNCATE TABLE dbo.GPS_Records";

        WebUtility.excutesql(sql);
        AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('删除成功','error','true',5);init();Loader.hide();");
    }

    protected void delGPSReclord_Click(object sender, EventArgs e)
    {
        Button lb = (Button)sender;
        int day = Convert.ToInt32(lb.CommandArgument.ToString());

        string sql = "DELETE FROM dbo.GPS_Records WHERE AddTime < '" + DateTime.Now.AddDays(-day) + "'";

        WebUtility.excutesql(sql);
        AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('删除成功','error','true',5);init();Loader.hide();");
    }

    /// <summary>
    /// 批量设置基本工资
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void setsalary_Click(object sender, EventArgs e)
    {
        Button bt = (Button)sender;
        string sql = "update Deliver set reveint2 = " + tbaddsalary.Text + " where ";
        if (bt.ID == "btselectadd")
        {
            string IdList = this.hidDels.Value;
            sql += " DataId in (" + IdList + ")";
        }
        else
        {
            sql += " " + SqlWhere;
        }

        if (WebUtility.excutesql(sql) > 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('设置成功','text:操作成功!','250','150','true','1000','true','text');init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('设置失败','text:操作失败!','250','150','true','1000','true','text');init();");
        }
    }

    /// <summary>
    /// 批量设置提成比例
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void setrat_Click(object sender, EventArgs e)
    {
        Button bt = (Button)sender;
        string sql = "update Deliver set reveint3 = " + this.tbrat.Text + " where ";
        if (bt.ID == "tbpart")
        {
            string IdList = this.hidDels.Value;
            sql += " DataId in (" + IdList + ")";
        }
        else
        {
            sql += " " + SqlWhere;
        }

        if (WebUtility.excutesql(sql) > 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('设置成功','text:操作成功!','250','150','true','1000','true','text');init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('设置失败','text:操作失败!','250','150','true','1000','true','text');init();");
        }
    }


    protected void btExport_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator_super(7);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }

        WebUtility.FixsetCookie("webStatisticsExcleOrder", Server.UrlEncode(SqlWhere), 1);
        Response.Redirect("deliverToExcel.aspx");
    }
}
